package com.chunuo.blogs.factory;

import org.commonmark.Extension;
import org.commonmark.node.Image;
import org.commonmark.node.Link;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.AttributeProvider;
import org.commonmark.renderer.html.AttributeProviderContext;
import org.commonmark.renderer.html.AttributeProviderFactory;
import org.commonmark.renderer.html.HtmlRenderer;

import java.util.*;

/**
 * 博客含有富文本，该类辅助富文本
 * （现在不会选这款技术了）
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-05-24 15:50
 */
public class MarkdownFactory {

    public static String markdownBasic(String body){
        Parser parser = Parser.builder().build();
        Node document = parser.parse(body);

        HtmlRenderer renderer = HtmlRenderer.builder().build();

//        HtmlRenderer renderer = HtmlRenderer.builder().
//                attributeProviderFactory(new AttributeProviderFactory() {
//                    @Override
//                    public AttributeProvider create(AttributeProviderContext attributeProviderContext) {
//                        return new PAttributeProvider();
//                    }
//                }).build();

        return renderer.render(document);
    }

    static class PAttributeProvider implements AttributeProvider {
        @Override
        public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
//            if (node instanceof Image) {
//                attributes.put("style", "width:150px;height:200px;position:relative;left:50%;margin-left:-100px;");
//            }
            if(node instanceof Link){
                attributes.put("target", "_blank");
            }
        }
    }

}
